﻿#include "DataToImageThread.h"

QImage DataToImageThread::m_image(1920, 1080, QImage::Format_ARGB32);
bool DataToImageThread::m_IsRead = false;
QMutex DataToImageThread::m_Mutex;

DataToImageThread::DataToImageThread()
    :m_Width(ImageDefaultWidth), m_Height(ImageDefaultHeight), m_Pixel(ImageDefaultPixel)
{

}

void DataToImageThread::run()
{
    // 被唤醒后说明获取数据能够生成一张图片()
    while (1)
    {
        {
            QMutexLocker locker(&m_mutex);
            m_Wait.wait(&m_mutex);
            // std::cout << "开始生成图片" << std::endl;
            {
                QMutexLocker locker1(&DataToImageThread::m_Mutex);
                for (uint32_t i = 0; i < m_Width * m_Height * m_Pixel; i += m_Pixel)
                {
                    // printf("0x%02X ", static_cast<int>(*(tmpPbuffer + i)));
                    // PixelVector.append(QColor(*(tmpPbuffer + i), *(tmpPbuffer + i + 1), *(tmpPbuffer + i + 2)));
                    DataToImageThread::m_image.setPixel((i / m_Pixel) % 1920, (i / m_Pixel) / 1920, QColor(*(m_tmpPbuffer + i), *(m_tmpPbuffer + i + 1), *(m_tmpPbuffer + i + 2)).rgba());
                }
                DataToImageThread::m_IsRead = true;
            }
            // qDebug() << "转换成功";
        }
         // qDebug() << "修改CeshiReadThread::m_IsRead后的值: " << DataToImageThread::m_IsRead;
    }
}

void DataToImageThread::setRead(uint8_t *tmpPbuffer)
{
    m_tmpPbuffer = tmpPbuffer;
    QMutexLocker locker(&m_mutex);
    m_Wait.wakeAll();
}
